<?php
/**
 * Created by PhpStorm.
 * User: dingjinlai
 * Date: 2019/8/9
 * Time: 下午2:38
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class EccApi extends OAuth_Controller
{
    public function add_order()
    {


        $this->load->library('eccang');

        $this->load->model('order_model');

        $data = $this->order_model->ecc_order();

        foreach ($data['orders'] as $item) {
            //$item['country_code'] = $data['address'][$item['shipping_id']];

            $region = $this->order_model->region(array('region_name' => ucwords($item['country'])));

            $item['country_code'] = $region['code'];
            $re_order = $this->eccang->syncOrder($item);
            $re_order = $this->eccang->syncOrder($item);

            if ($re_order['code'] == 200) {
                $this->order_model->update_field(array('ecc_code' => $re_order['data']['saleOrderCode']), $item['order_id']);
            } else {
                print_r(ucwords($item['country']));
                print_r($region);
                print_r($re_order);
            }

            sleep(5);
        }

    }

    public function e_sku(){

        ignore_user_abort();
        set_time_limit(0);

        $this->load->model('suppliers_model');
        $this->load->model('goods_sku_model');
        $this->load->model('epurchase_orders_model');

        $this->load->library('eccang');

        $assoc = $this->uri->uri_to_assoc();

        $i = 1;
        while($i <= 55){
            $ecc_pur_order = $this->eccang->getPurchaseOrders(array(
                'pageSize' => '500',
                'page' => $i
            ));


            $_data = array();
            foreach ($ecc_pur_order['data'] as $data) {

                $supplier = $this->suppliers_model->suppliers_info(array(
                    'e_code' => $data['supplier_code']
                ));

                $_data[] = array(
                    'po_id' => $data['po_id'],
                    'po_code' => $data['po_code'],
                    'tracking_no' => $data['tracking_no'],
                    'ref_no' => $data['ref_no'],
                    'payable_amount' => $data['payable_amount'],
                    'actually_amount' => $data['actually_amount'],
                    'currency_code' => trim($data['currency_code']),
                    'pay_status' => intval($data['pay_status']),
                    'po_staus' => intval($data['po_staus']),
                    'date_create' => strtotime($data['date_create']),
                    'date_eta' => strtotime($data['date_eta']),
                    'date_release' => strtotime($data['date_release']),
                    'po_completion_time' => strtotime($data['po_completion_time']),
                    'return_verify' => urldecode($data['return_verify']),
                    'pay_ship_amount' => floatval($data['pay_ship_amount']),
                    'create_type' => intval($data['create_type']),
                    'pts_status_sort' => $data['pts_status_sort'],
                    'account_type' => intval($data['account_type']),
                    'transaction_no' => $data['transaction_no'],
                    'supplier_id' => $supplier['supplier_id'],
                    'verify' => $data['verify'],
                    'qty_expected_all' => $data['qty_expected_all'],
                    'qty_receving_all' => $data['qty_receving_all'],
                    'po_is_net' => $data['po_is_net'],
                    'purchase_json' => json_encode($data)
                );

                foreach ($data['detail'] as $detail) {
                    $goods_sku = $this->goods_sku_model->sku_info("REPLACE(')', '', REPLACE('(', '', REPLACE('/', '', sku_sn))) = '{$detail['product_sku']}'");
                    $_data_goods[] = array(
                        'po_id' => $data['po_id'],
                        'goods_id' => $goods_sku['goods_id'],
                        'sku_id' => $goods_sku['sku_id'],
                        'qty_expected' => $detail['qty_expected'],
                        'qty_pay' => $detail['qty_pay'],
                        'qty_eta' => $detail['qty_eta'],
                        'qty_receving' => $detail['qty_receving'],
                        'qty_free' => $detail['qty_free'],
                        'unit_price' => floatval($detail['unit_price']),
                        'total_price' => floatval($detail['total_price']),
                        'currency_code' => trim($detail['currency_code']),
                        'product_sku' => $detail['product_sku'],
                        'first_receive_time' => strtotime($detail['first_receive_time']),
                    );
                }

            }

            $this->epurchase_orders_model->insert_batch($_data);
            $this->epurchase_orders_model->goods_insert_batch($_data_goods);

            $i++;
            $_data_goods = null;
            $_data = null;
            $ecc_pur_order = null;
            sleep(40);
        }
    }

    public function e_sale_goods()
    {
        $this->load->model('goods_sku_model');
        $this->load->library('eccang');



        $i = 1;
        while($i <= 3){
            $sale_sku = $this->eccang->getProductList(array(
                'saleStatus' => 1,
                'pageSize' => 500,
                'page' => 1
            ));

            foreach ($sale_sku['data'] as $data) {
                $sku_sn = $sku_data[$data['productSku']] ? $sku_data[$data['productSku']] : $data['productSku'];
                $this->goods_sku_model->salse_update(array(
                    'is_on_sale' => 0,
                    'on_sale_time' => strtotime($data['productUpdateTime'])
                ), $sku_sn);
            }

            sleep(10);
            $i++;
        }

    }

    public function today_ecc_po(){

        ignore_user_abort();
        set_time_limit(0);

        $this->load->model('suppliers_model');
        $this->load->model('goods_sku_model');
        $this->load->model('epurchase_orders_model');

        $today = strtotime('today');
        $data['searchDateType'] = 'createDate';
        $data['dateFor'] = date("Y-m-d", $today - (24 * 3600));
        $data['dateTo'] =  date("Y-m-d", $today);
        $data['pageSize'] = '1000';

        $this->load->library('eccang');
        $ecc_pur_order = $this->eccang->getPurchaseOrders($data);

        if ($ecc_pur_order['data']) {
            $_data = array();
            foreach ($ecc_pur_order['data'] as $data) {

                $supplier = $this->suppliers_model->suppliers_info(array(
                    'e_code' => $data['supplier_code']
                ));

                $_data[] = array(
                    'po_id' => $data['po_id'],
                    'po_code' => $data['po_code'],
                    'tracking_no' => $data['tracking_no'],
                    'ref_no' => $data['ref_no'],
                    'payable_amount' => $data['payable_amount'],
                    'actually_amount' => $data['actually_amount'],
                    'currency_code' => trim($data['currency_code']),
                    'pay_status' => intval($data['pay_status']),
                    'po_staus' => intval($data['po_staus']),
                    'date_create' => strtotime($data['date_create']),
                    'date_eta' => strtotime($data['date_eta']),
                    'date_release' => strtotime($data['date_release']),
                    'po_completion_time' => strtotime($data['po_completion_time']),
                    'return_verify' => urldecode($data['return_verify']),
                    'pay_ship_amount' => floatval($data['pay_ship_amount']),
                    'create_type' => intval($data['create_type']),
                    'pts_status_sort' => $data['pts_status_sort'],
                    'account_type' => intval($data['account_type']),
                    'transaction_no' => $data['transaction_no'],
                    'supplier_id' => $supplier['supplier_id'],
                    'verify' => $data['verify'],
                    'qty_expected_all' => $data['qty_expected_all'],
                    'qty_receving_all' => $data['qty_receving_all'],
                    'po_is_net' => $data['po_is_net'],
                    'purchase_json' => json_encode($data)
                );

                foreach ($data['detail'] as $detail) {
                    $goods_sku = $this->goods_sku_model->sku_info("REPLACE(')', '', REPLACE('(', '', REPLACE('/', '', sku_sn))) = '{$detail['product_sku']}'");
                    $_data_goods[] = array(
                        'po_id' => $data['po_id'],
                        'goods_id' => $goods_sku['goods_id'],
                        'sku_id' => $goods_sku['sku_id'],
                        'qty_expected' => $detail['qty_expected'],
                        'qty_pay' => $detail['qty_pay'],
                        'qty_eta' => $detail['qty_eta'],
                        'qty_receving' => $detail['qty_receving'],
                        'qty_free' => $detail['qty_free'],
                        'unit_price' => floatval($detail['unit_price']),
                        'total_price' => floatval($detail['total_price']),
                        'currency_code' => trim($detail['currency_code']),
                        'product_sku' => $detail['product_sku'],
                        'first_receive_time' => strtotime($detail['first_receive_time']),
                    );
                }

            }

            $this->epurchase_orders_model->insert_batch($_data);
            $this->epurchase_orders_model->goods_insert_batch($_data_goods);
        }
    }

    public function add_goods()
    {
        $this->load->library('eccang');
    }
}
